<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>cr-pay-mall Login</title>
    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f4;
        }
        .container {
            text-align: center;
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }
        h1 {
            font-size: 2em;
            margin-bottom: 20px;
            color: #333;
        }
        .qrcode {
            width: 200px;
            height: 200px;
            margin: 0 auto;
            border: 2px solid #ddd;
            border-radius: 8px;
            overflow: hidden;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #f9f9f9;
        }
        .qrcode img {
            width: 100%;
            height: 100%;
            object-fit: contain;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>cr-pay-mall 商城登录页</h1>
    <div class="qrcode">
        <img id="qrcode-img" src="./images/placeholder.png" alt="Loading QR Code...">
    </div>
    <p class="instructions">请使用微信扫描二维码登录</p>
</div>

<script>
    const placeholderImage = "./images/placeholder.png";
    const apiGetTicket = "http://rowntit.natapp1.cc/api/v1/login/weixin_qrcode_ticket";
    const apiCheckLogin = "http://rowntit.natapp1.cc/api/v1/login/check_login?ticket=";
    let ticket = null;
    let pollingInterval = null;

    // 获取微信登录二维码
    async function fetchQRCode() {
        try {
            const response = await fetch(apiGetTicket);
            const result = await response.json();
            if (result.code === "0000" && result.data) {
                ticket = result.data;
                const qrCodeUri = `https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=${ticket}`;
                document.getElementById("qrcode-img").src = qrCodeUri;

                // 开始轮询登录状态
                startPolling(ticket);
            } else {
                console.error("获取登录二维码失败:", result.message);
            }
        } catch (error) {
            console.error("获取登录二维码时发送错误:", error);
        }
    }

    // 轮询登录状态
    async function checkLoginStatus(ticket) {
        try {
            const response = await fetch(`${apiCheckLogin}${ticket}`);
            const result = await response.json();
            if (result.code === "0000" && result.data) {
                // 登录成功，存储token到cookie
                document.cookie = `openIdToken=${result.data}; path=/`;
                clearInterval(pollingInterval);
                window.location.href = "index.html"; // 跳转到首页
            } else {
                console.log("未获取到登录状态:", result.message);
            }
        } catch (error) {
            console.error("获取登录状态时发生了错误:", error);
        }
    }

    // 开始轮询
    function startPolling(ticket) {
        pollingInterval = setInterval(() => {
            checkLoginStatus(ticket);
        }, 3000); // 每3秒轮询一次
    }

    // 页面加载后获取二维码
    window.onload = fetchQRCode;
</script>
</body>
</html>
